package com.mysql.injector.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.mysql.injector.mapper.SuperMapper;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 基于MP的 IService 新增了1个方法： saveBatchSomeColumn 其中： 1，saveBatchSomeColumn 批量插入
 *
 * @param <T> 实体
 */
public interface SuperService<T> extends IService<T> {

  /**
   * 批量保存数据
   * <p>
   * 注意：该方法仅仅测试过mysql
   *
   * @param entityList 实体对象集合
   * @return 是否成功
   */
  default boolean saveBatchSomeColumn(List<T> entityList) {
    return SqlHelper.retBool(saveBatchSomeColumnReturnValue(entityList));
  }

  default boolean replaceIntoBatchSomeColumn(List<T> entityList) {
    return SqlHelper.retBool(replaceIntoBatchSomeColumnReturnValue(entityList));
  }

  default boolean insertIgnoreBatchSomeColumn(List<T> entityList) {
    return SqlHelper.retBool(insertIgnoreBatchSomeColumnReturnValue(entityList));
  }

  default boolean insertIgnore(T entity) {
    return ((SuperMapper)getBaseMapper()).insertIgnore(entity) > 0;
  }

  default boolean insertOnDuplicateKeyUpdateBatchSomeColumn(List<T> entityList, Collection<String> columnList) {
    return SqlHelper.retBool(insertOnDuplicateKeyUpdateBatchSomeColumnReturnValue(entityList, columnList));
  }

  default boolean insertOnDuplicateKeyUpdateBatchSomeColumn(List<T> entityList, Map<String, Object> columnMap) {
    return SqlHelper.retBool(insertOnDuplicateKeyUpdateBatchSomeColumnReturnValue(entityList, columnMap));
  }

  default boolean insertOnDuplicateKeyUpdate(T entity) {
    return ((SuperMapper)getBaseMapper()).insertOnDuplicateKeyUpdate(entity) > 0;
  }

  default boolean insertOnDuplicateKeyUpdate(T entity, Collection<String> columnList) {
    return ((SuperMapper)getBaseMapper()).insertOnDuplicateKeyUpdate(entity, columnList) > 0;
  }

  default boolean insertOnDuplicateKeyUpdate(T entity, Map<String, Object> columnMap) {
    return ((SuperMapper)getBaseMapper()).insertOnDuplicateKeyUpdate(entity, columnMap) > 0;
  }

  default int saveBatchSomeColumnReturnValue(List<T> entityList) {
    if (entityList.isEmpty()) {
      return 0;
    }
    if (entityList.size() > 5000) {
      throw new RuntimeException("批量插入长度不能超过5000条");
    }
    return ((SuperMapper)getBaseMapper()).insertBatchSomeColumn(entityList);
  }

  default int replaceIntoBatchSomeColumnReturnValue(List<T> entityList) {
    if (entityList.isEmpty()) {
      return 0;
    }
    if (entityList.size() > 5000) {
      throw new RuntimeException("批量插入长度不能超过5000条");
    }
    return ((SuperMapper)getBaseMapper()).replaceIntoBatchSomeColumn(entityList);
  }

  default int insertIgnoreBatchSomeColumnReturnValue(List<T> entityList) {
    if (entityList.isEmpty()) {
      return 0;
    }
    if (entityList.size() > 5000) {
      throw new RuntimeException("批量插入长度不能超过5000条");
    }
    return ((SuperMapper)getBaseMapper()).insertIgnoreBatchSomeColumn(entityList);
  }

  default int insertOnDuplicateKeyUpdateBatchSomeColumnReturnValue(List<T> entityList, Collection<String> columnList) {
    if (entityList.isEmpty()) {
      return 0;
    }
    if (entityList.size() > 5000) {
      throw new RuntimeException("批量插入长度不能超过5000条");
    }
    return ((SuperMapper)getBaseMapper()).insertOnDuplicateKeyUpdateBatchSomeColumn(entityList, columnList);
  }

  default int insertOnDuplicateKeyUpdateBatchSomeColumnReturnValue(List<T> entityList, Map<String, Object> map) {
    if (entityList.isEmpty()) {
      return 0;
    }
    if (entityList.size() > 5000) {
      throw new RuntimeException("批量插入长度不能超过5000条");
    }
    return ((SuperMapper)getBaseMapper()).insertOnDuplicateKeyUpdateBatchSomeColumn(entityList, map);
  }
}
